package net.betou.trade.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.trade.action.abs.ActionSupport;
import net.betou.trade.entity.main.SuggestionType;
import net.betou.trade.main.manager.SuggestionTypeMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;
import net.betou.trade.web.WebErrors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 意见分类
 * @author O.Y.
 */
@Controller
public class SuggestionTypeAct extends ActionSupport< SuggestionType >{
	
	@RequestMapping( "/suggestiontype/v_list.jspx" )
	public String v_list( String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		List< SuggestionType > list = manager.findList(orderProperty, orderDirection);
		this.paramToModel( model, null, list, null, null, null, orderProperty, orderDirection, null );
		return getListPage( );
	}
	
	@RequestMapping( "/suggestiontype/v_add.jspx" )
	public String v_add( HttpServletRequest request, ModelMap model ){
		return getAddPage( );
	}
	
	@RequestMapping( "/suggestiontype/o_save.jspx" )
	public String o_save( SuggestionType bean,  HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		WebErrors errors = validateSave( bean );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		bean = manager.save( bean );
		
		logMng.operating( 0, request, Messages.LOG_SUGGESTION_TYPE, "id=" + bean.getTypeId() + ",name=" + bean.getName( ) );
		
		return "redirect:v_list.jspx";
	}
	
	@RequestMapping ( "/suggestiontype/v_edit.jspx" )
	public String v_edit( Integer id, HttpServletRequest request, ModelMap model ){

		WebErrors errors = validateUpdate( request, id );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		SuggestionType bean = manager.findById( id );
		
		model.addAttribute( "bean", bean );
		
		return getUpdatePage( );
	}
	
	@RequestMapping ( "/suggestiontype/o_update.jspx" )
	public String o_update(  Integer typeId, SuggestionType bean, HttpServletRequest request, ModelMap model ){
		
		WebErrors errors = validateUpdate( request, typeId );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		bean = manager.update( bean );
		
		logMng.operating( 1, request, Messages.LOG_SUGGESTION_TYPE, "id=" + bean.getTypeId( ) + ",name=" + bean.getName( ));
		
		return "redirect:v_list.jspx";
	}

	@RequestMapping( "/suggestiontype/o_delete.jspx" )
	public void o_delete( Integer [ ] ids, HttpServletResponse response, HttpServletRequest request ){
		
		ResponseUtil util = validateDelete( ids,  response, request );

		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}

		SuggestionType [ ] beans = manager.deleteByIds( ids );

		for ( SuggestionType bean : beans ){
			logMng.operating( 2, request, Messages.LOG_SUGGESTION_TYPE, "id=" + bean.getTypeId( ) + ",name=" + bean.getName( ));
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );
		return;
		
	}
	
	private ResponseUtil validateDelete( Integer [ ] ids, HttpServletResponse response, HttpServletRequest request ) {
		ResponseUtil util = ResponseUtil.create( response );

		if( ids == null || ids.length == 0 ){
			util.addErrorResponse( Messages.DATA_NOT_SELECT );
			return util;
		}

		for ( Integer id : ids ){
			SuggestionType bean = manager.findById( id );
			
			if( bean == null ){
				util.addErrorResponse(  Messages.DATA_NOT_EXIST  );
				break;
			}
		}
		return util;
	}
	
	private WebErrors validateUpdate( HttpServletRequest request, Integer id ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		SuggestionType bean = manager.findById( id );
		
		if( bean == null ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		return errors;
			
	}
	
	private WebErrors validateSave( SuggestionType bean ){
		WebErrors errors = WebErrors.create( );
		if(bean.getTypeId()!=null){
			SuggestionType oldBean = manager.findById( bean.getTypeId() );
			if(oldBean!=null){
				errors.addError("类型ID不能重复");
			}
		}
		
		return errors;
	}

	@Autowired
	private SuggestionTypeMng manager;
	
	private static final String DIRECTORY = "suggestion/type";
	@Override
	protected String getDirectory() {
		return DIRECTORY;
	}

}
